<?php
/**
 * Created by Pako
 * Date: 2019/2/8
 * Time: 17:09
 */

namespace App\Repositories\Product;


use App\Models\Product\TemplateAttr;
use App\Models\Product\TypeTemplate;
use App\Repositories\Repository;

class TemplateRepository extends Repository
{
    public function index()
    {
        $template = TypeTemplate::query()->paginate($this->page_size, ['*'], 'page', $this->page);
        return $this->respondWith($template);
    }

    public function show($template_id)
    {
        $template = TypeTemplate::query()->find($template_id);
        return $this->respondWith($template);
    }

    public function store($data)
    {
        $data['attribues'] = json_encode($data['attribues']);
        $data['brands'] = json_encode($data['brands']);
        $template_attrs = [];
        if (isset($data['custom_attribues'])) {
            $template_attrs = $data['custom_attribues'];
            $data['custom_attribues'] = json_encode($data['custom_attribues']);
        }
        try {
            $template = TypeTemplate::query()->create($data);
            if (count($template_attrs) > 0) {
                $this->createTemplateAttr($template->template_id, $template_attrs);
            }
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10110);
        }
    }

    public function save($params)
    {
        $template = TypeTemplate::query()->find($params['template_id']);
        if (!$template) {
            $this->throwRespondError(10109);
        }
        if (isset($params['attribues']) && count($params['attribues']) > 0) {
            $this->createTemplateAttr($template->template_id, $params['attribues']);
            $params['attribues'] = json_encode($params['attribues']);
        }
        if (isset($params['brands'])) {
            $params['brands'] = json_encode($params['brands']);
        }
        if (isset($params['custom_attribues'])) {
            $params['custom_attribues'] = json_encode($params['custom_attribues']);
        }

        try {
            $template->fill($params)->save();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10108);
        }
    }

    public function del($template_ids)
    {
        if (!is_array($template_ids)) {
            $template_ids = explode(',', $template_ids);
        }
        try {
            TypeTemplate::query()->whereIn('template_id', $template_ids)->delete();
            TemplateAttr::query()->whereIn('template_id', $template_ids)->delete();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10107);
        }
    }

    private function createTemplateAttr($template_id, $attrs, $update_attrs = [])
    {
        TemplateAttr::query()->where('template_id', $template_id)->delete();
        $data = [];
        foreach ($attrs as $attr) {
            $data[] = ['template_id' => $template_id, 'attr_id' => $attr['attr_id']];
        }
        if (count($update_attrs)) {
            foreach ($update_attrs as $attr) {
                $data[] = ['template_id' => $template_id, 'attr_id' => $attr['attr_id']];
            }
        }
        TemplateAttr::query()->insert($data);
    }

}